/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.esgi.dns;

import java.nio.ByteBuffer;
import org.esgi.dns.Message.DomainNameLengthException;
import org.esgi.dns.Message.LabelLengthException;

/**
 * This class is used to create bytes arrays according to the standards given in
 * the CLASS field in a Ressource Record (RR).
 *
 * Supported CLASS :
 *
 * - CNAME RDATA format                 : YES 
 * - HINFO RDATA format                 : NO 
 * - MB RDATA format (EXPERIMENTAL)     : NO 
 * - MD RDATA format (Obsolete)         : NO 
 * - MF RDATA format (Obsolete)         : NO 
 * - MG RDATA format (EXPERIMENTAL)     : NO 
 * - MINFO RDATA format (EXPERIMENTAL)  : NO 
 * - MR RDATA format (EXPERIMENTAL)     : NO 
 * - MX RDATA format                    : NO 
 * - NULL RDATA format (EXPERIMENTAL)   : NO 
 * - NS RDATA format                    : NO 
 * - PTR RDATA format                   : NO 
 * - SOA RDATA format                   : NO 
 * - TXT RDATA format                   : NO
 *
 * ----Internet specific RRs---- 
 * - A RDATA format                     : YES 
 * - WKS RDATA format                   : NO
 *
 *
 * http://www.ietf.org/rfc/rfc1035.txt (§3.3. Standard RRs)
 *
 * @author Alain Janin-Manificat <alain.janinm@hotmail.fr>
 */
public class RData {

    /**
     * 3.3.1. CNAME RDATA format
     *
     * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ / CNAME / / /
     * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
     *
     * where:
     *
     * CNAME : A <domain-name> which specifies the canonical or primary name for
     * the owner. The owner name is an alias.
     *
     * @param CNAME String - A <domain-name> which specifies the canonical or
     * primary name for the owner. The owner name is an alias.
     * @param message ByteBuffer - The buffer containing the current message.
     */
    public static void createCNameRData(String CNAME, ByteBuffer message) throws LabelLengthException, DomainNameLengthException {

        /*
         * Check if a pointer to given label exists. If true the pointer exists
         * and has been set.
         */
        if (!Message.setPointer(CNAME, message)) {
            Message.setDomainName(CNAME, message);
        }
    }

    /**
     * 3.4.1. A RDATA format
     *
     * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ADDRESS |
     * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
     *
     * where:
     *
     * ADDRESS A 32 bit Internet address.
     *
     * Hosts that have multiple Internet addresses will have multiple A records.
     *
     *
     * @param address byte[] - 32 bits address.
     * @param message ByteBuffer - ByteBuffer containing current message.
     * @throws org.esgi.dns.RData.InvalidInternetAddress
     */
    public static void createARData(byte[] address, ByteBuffer message) throws InvalidInternetAddress {
        if (address.length != 4) {
            throw new InvalidInternetAddress();
        }
        message.put(address);
    }

    /**
     * Exception for Internet address length different from 4 bytes.
     */
    public static class InvalidInternetAddress extends Exception {

        public InvalidInternetAddress() {
            super("A valid Internet address is 32 bits length (4 bytes).");
        }
    }
}
